#! /bin/bash

# Note this script should be run in the project's top directory

# TO DO: print help string

LINUX=$1
if [ -z "${LINUX}"]; then
    LINUX="linux-6.1"
fi

SRC_ROOT=`pwd`
LINUX_SRC_ROOT=${SRC_ROOT}/external/kernel/linux/${LINUX}

git submodule init && git submodule
if [ $? -ne 0 ]; then
    echo "ERROR: failed to update submodules"
    exit -1
fi

if [ ! -d ${LINUX_SRC_ROOT} ]; then
    echo "ERROR: ${LINUX} source not found"
    exit -1
fi

cp ${SRC_ROOT}/configs/${LINUX}.config ${LINUX_SRC_ROOT}/.config

cd ${LINUX_SRC_ROOT}
NRPROC=$(cat /proc/cpuinfo | grep processor | wc -l)
if [ ${NRPROC} -gt 2 ]; then
    NRPROC=`expr ${NRPROC} - 2`
fi
make -j${NRPROC} bzImage
cd ${SRC_ROOT}
